

<!-- 富文本内容 -->
<template>
	<view class="richText-Container">
		<rich-text :nodes="text"></rich-text>
	</view>
</template>

<script>
export default {
	onLoad(options) {
		if(options.bannerId) {
			this.findBannerByIdApi_fun(options.bannerId);
		}else {
			this.text = this.formatRichText(options.text);
			uni.setNavigationBarTitle({
				title: options.title
			});
		}
	},
	data() {
		return {
			text: ""
		}
	},
	methods:{

		//获取富文本内容
		findBannerByIdApi_fun(bannerId) {
			var that = this;
			that.$api.globalUtil.requestAjax({
				url: "/bannerApi/findBannerByIdApi",
				method: "POST",
				data: {
					bannerId: bannerId
				},
				success: function(res) {
					if(res.data.success) {
						var banner = res.data.response;

						uni.setNavigationBarTitle({
							title: banner.bannerDesc
						});
						that.text = that.formatRichText(banner.content);
					}
				}
			});
		},


		//富文本格式化
		formatRichText(html) {
			
			html = html || "";
			
			//控制小程序中图片大小
			let newContent = html.replace(/<img[^>]*>/gi, function(match, capture) {
				match = match.replace(/style="[^"]+"/gi, '').replace(/style='[^']+'/gi, '');
				match = match.replace(/width="[^"]+"/gi, '').replace(/width='[^']+'/gi, '');
				match = match.replace(/height="[^"]+"/gi, '').replace(/height='[^']+'/gi, '');
				return match;
			});
			newContent = newContent.replace(/style="[^"]+"/gi, function(match, capture) {
				match = match.replace(/width:[^;]+;/gi, 'max-width:100%;').replace(/width:[^;]+;/gi, 'max-width:100%;');
				return match;
			});
			// newContent = newContent.replace(/<br[^>]*\/>/gi, '');
			newContent = newContent.replace(/\<img/gi, '<img style="max-width:100%;height:auto;display:block;margin:0 auto;"');
			return newContent;
		}
	}
}
</script>

<style>
</style>



